﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 文件浏览
{
    public partial class log : System.Web.UI.Page
    {
        StreamWriter sw = null;
        string dir = "";
        LinkedList<string> ll = new LinkedList<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            dir = Server.MapPath(".")+ "\\logs";
            TextBox1.Enabled = false;
            if(!IsPostBack)
                Scanlist();
            //Label1.Text = dir;
        }

        //新建 log 按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            //开启保存按钮
            Button2.Enabled = true;
            TextBox1.Enabled = true;
            string currtime = DateTime.Now.ToString("MM-dd-yy HH.mm.ss");
            Label1.Text = "log创建时间: " + currtime;

        }
        //保存按钮
        protected void Button2_Click(object sender, EventArgs e)
        {
            string currtime = DateTime.Now.ToString("MM-dd-yy HH.mm.ss");
            string sdir = dir + "\\" + currtime + ".txt";

            sw = new StreamWriter(sdir);
            sw.Write(TextBox1.Text);
            sw.Close();

            TextBox1.Text = "";
            TextBox1.Enabled = false;
            Label1.Text = " 已保存";

            Scanlist();

            //关闭保存按钮
            Button2.Enabled = false;

        }
        //删除按钮
        protected void Button4_Click(object sender, EventArgs e)
        {
            string sdir = dir + "\\" + ListBox1.SelectedValue;
            if (File.Exists(sdir))
            {
                File.Delete(sdir);
            }
            Label1.Text = "新建 / 选择一个log";
            TextBox1.Text = "Log 删除成功";
            TextBox1.Enabled = false;
            Scanlist();
        }
        private void Scanlist()
        {
            
            ListBox1.Items.Clear();
            DirectoryInfo TheFolder = new DirectoryInfo(dir);
            foreach (var item in TheFolder.GetFiles())
            {
                ListBox1.Items.Add(item.Name);
            }

            DataBind();
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //关闭保存按钮
            Button2.Enabled = false;

            string sdir = dir + "\\" + ListBox1.SelectedValue;
            Label1.Text = sdir;
            StreamReader sr = new StreamReader(sdir);
            string fileString = sr.ReadToEnd();
            //TextBox1
            //TextBox1.Enabled = true;
            TextBox1.Text = fileString;
            sr.Close();
        }
        //打开
        protected void Button5_Click(object sender, EventArgs e)
        {
            //关闭保存按钮
            Button2.Enabled = false;

            string sdir = dir + "\\" + ListBox1.SelectedValue;
            Label1.Text = sdir;
            StreamReader sr = new StreamReader(sdir);
            string fileString = sr.ReadToEnd();
            //TextBox1
            //TextBox1.Enabled = true;
            TextBox1.Text = fileString;
            sr.Close();
        }
    }
}